package com.hqyj.server.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.server.entity.Users;
import com.hqyj.server.mapper.UsersMapper;
import com.hqyj.server.service.UsersService;
import com.hqyj.server.util.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean reg(Users u) throws Exception {

        //对用户密码进行加密
        u.setPassword(MyUtil.encodePasswordByMd5Salt(u.getPassword()));
        //
        u.setGender("男"); //默认的性别
        //默认的头像
        u.setAvatar("https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg");
        //自动生成一个用户名
        u.setUsername(IdUtil.simpleUUID());
        u.setBirthday(DateUtil.today());
        return super.save(u);
    }

    @Override
    public Users login(String identify, String password) {
        try {
            QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();

            password = MyUtil.encodePasswordByMd5Salt(password);
            //说明是手机
            if (MyUtil.isMobileNumber(identify)) {
                queryWrapper.eq("mobile", identify);
            } else if (MyUtil.isEmail(identify)) {
                queryWrapper.eq("email", identify);
            } else {
                queryWrapper.eq("username", identify);
            }
            queryWrapper.eq("password",password);

            Users loginSuccessUser = super.getOne(queryWrapper);
            return loginSuccessUser;
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        return null;
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean updateUser(Users u) throws Exception {
        try{
            super.updateById(u);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            //抛出异常后，必须手动回滚。
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean checkIsReg(String identify) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();

        //说明是手机
        if(MyUtil.isMobileNumber(identify)){
            queryWrapper.eq("mobile",identify);
        }else if(MyUtil.isEmail(identify)){
            queryWrapper.eq("email",identify);
        }

        List<Users> usersList = super.list(queryWrapper);

        return usersList.size()>0;
    }
}
